/*jslint sloppy: true */
/*global require */
var probeURI = require('../uri').probeURI;
var probe = require('../global');
var angular = require('angular');

probe.service('Case', function ($resource) {

	/**
	 * {tags: null} -> {tags: []}
	 */
	function transformCaseRes(data) {
		var res = angular.fromJson(data);

		if (res && !res.tags) {
			res.tags = [];
		} else {
			res.tags = angular.fromJson(res.tags);
		}

		return res;
	}

	/**
	 * {tags: [1]} - > {tags: "[1]"}
	 */
	function transformCaseReq(data) {
		var req = angular.copy(data);
		req.tags = angular.toJson(data.tags);

		return angular.toJson(req);
	}

	return $resource(probeURI + 'case/:caseId', null, {
		list: {
			isArray: true,
			method: 'GET',
			params: { mask: 'code' },
			transformResponse: function (data) {
				return angular.fromJson(data).map(transformCaseRes);
			}
		},
		create: {
			method: 'POST',
			transformRequest: transformCaseReq,
			transformResponse: transformCaseRes
		},
		modify: {
			method: 'PUT',
			params: { caseId: '@caseId' },
			transformRequest: transformCaseReq,
			transformResponse: transformCaseRes
		},
//		remove: {
//			method: 'DELETE'
//		},
		get: {
			method: 'GET',
			params: { caseId: '@caseId' },
			transformResponse: transformCaseRes
		},
		query: {
			method: 'GET',
			isArray: true
		}
	});
}).service('Project', function ($resource) {
	return $resource(probeURI + 'project', null, {
		login: {
			method: 'POST',
			url: probeURI + 'login'
		},
		logout: {
			method: 'GET',
			url: probeURI + 'logout'
		},
		noop: {
			method: 'GET',
			url: probeURI + 'noop'
		}
	});
}).service('Task', function ($resource) {
	return $resource(probeURI + 'task/:taskId', null, {
		list: {
			method: 'GET',
			params: { mask: 'schemas' },
			isArray: true
		},
		create: {
			method: 'POST'
		},
		modify: {
			method: 'PUT',
			params: { taskId: '@taskId' }
		},
//		remove: {
//			method: 'DELETE'
//		},
		get: {
			method: 'GET',
			params: { taskId: '@taskId'}
		}
	});
}).service('Result', function ($resource) {
	return $resource(probeURI + 'result/', null, {
		upload: {
			method: 'POST'
		}
	});
});
